package com.ruoyi.instrument.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.instrument.mapper.InstrumentMapper;
import com.ruoyi.instrument.domain.Instrument;
import com.ruoyi.instrument.service.IInstrumentService;

/**
 * 仪器信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class InstrumentServiceImpl implements IInstrumentService
{
    @Autowired
    private InstrumentMapper instrumentMapper;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询仪器信息
     *
     * @param id 仪器信息主键
     * @return 仪器信息
     */
    @Override
    public Instrument selectInstrumentById(Long id)
    {
        Instrument i = instrumentMapper.selectInstrumentById(id);
        if(i != null){
            if(StringUtils.isNotEmpty(i.getPurpose()) && i.getPurpose().contains(",")){
                i.setPurposeList(i.getPurpose().split(","));
            }
        }
        return i;
    }

    /**
     * 查询仪器信息列表
     *
     * @param instrument 仪器信息
     * @return 仪器信息
     */
    @Override
    public List<Instrument> selectInstrumentList(Instrument instrument)
    {
        List<Instrument> instrumentList = instrumentMapper.selectInstrumentList(instrument);
        if(instrumentList != null && !instrumentList.isEmpty()){
            for(Instrument i:instrumentList){
                if(StringUtils.isNotEmpty(i.getPurpose()) && i.getPurpose().contains(",")){
                    i.setPurposeList(i.getPurpose().split(","));
                }
            }
        }
        return instrumentList;
    }

    /**
     * 查询仪器使用记录列表
     *
     * @param instrument 仪器信息
     * @return 仪器信息
     */
    @Override
    public  List<Map<String,Object>> selectInstrumentRecordList(Instrument instrument)
    {
        return instrumentMapper.selectInstrumentRecordList(instrument);
    }

    /**
     * 新增仪器信息
     *
     * @param instrument 仪器信息
     * @return 结果
     */
    @Override
    public int insertInstrument(Instrument instrument)
    {
        instrument.setCreateTime(DateUtils.getNowDate());
        return instrumentMapper.insertInstrument(instrument);
    }

    /**
     * 新增仪器使用记录信息
     *
     * @param instrument 仪器信息
     * @return 结果
     */
    @Override
    public int insertInstrumentRecord(Instrument instrument)
    {
        instrument.setCreateTime(DateUtils.getNowDate());
        return instrumentMapper.insertInstrumentRecord(instrument);
    }

    /**
     * 修改仪器信息
     *
     * @param instrument 仪器信息
     * @return 结果
     */
    @Override
    public int updateInstrument(Instrument instrument)
    {
        instrument.setUpdateTime(DateUtils.getNowDate());
        return instrumentMapper.updateInstrument(instrument);
    }

    /**
     * 批量删除仪器信息
     *
     * @param ids 需要删除的仪器信息主键
     * @return 结果
     */
    @Override
    public int deleteInstrumentByIds(Long[] ids)
    {
        return instrumentMapper.deleteInstrumentByIds(ids);
    }


    /**
     * 批量逻辑删除仪器信息
     *
     * @param ids 需要删除的仪器信息主键
     * @return 结果
     */
    @Override
    public int logicDeleteInstrument(Long[] ids,String delType,Long delUser)
    {
        return instrumentMapper.logicDeleteInstrument(ids,delType,delUser);
    }

    /**
     * 删除仪器信息信息
     *
     * @param id 仪器信息主键
     * @return 结果
     */
    @Override
    public int deleteInstrumentById(Long id)
    {
        return instrumentMapper.deleteInstrumentById(id);
    }


    /**
     * 导入用户数据
     *
     * @param instrumentList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importInstrument(List<Instrument> instrumentList, Boolean isUpdateSupport)
    {
        if (StringUtils.isEmpty(instrumentList))
        {
            throw new ServiceException("导入仪器信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Instrument instrument : instrumentList)
        {
            try
            {
                // 验证是否存在这个仪器
                Instrument i = instrumentMapper.selectInstrumentByName(instrument.getInstrumentName());
                if (StringUtils.isNull(i))
                {

                    this.insertInstrument(instrument);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、仪器 " + instrument.getInstrumentName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    this.updateInstrument(instrument);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、仪器 " + instrument.getInstrumentName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、仪器 " + instrument.getInstrumentName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、仪器 " + instrument.getInstrumentName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
